package com.jy.api.service.impl;

import com.jy.api.dao.ElectronicBillDao;
import com.jy.api.domain.ElectronicBill;
import com.jy.api.domain.ResELC;
import com.jy.api.service.ElectronicBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import sun.util.resources.ga.LocaleNames_ga;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by xiaohua on 2018/11/7.
 */

@Slf4j
@Service
public class ElectronicBillServiceImpl implements ElectronicBillService {

    @Autowired
    ElectronicBillDao electronicBillDao;


    @Override
    public Map<String, Object> findTotalAmount(String storeId, String from, String to, Pageable pageable) {
        from=from+" 00:00:00";
        to=to+" 23:59:59";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Date start = null;
        Date end = null;
        Map<String, Object> map = new HashMap<>();
        try {
            start = simpleDateFormat.parse(from);
            end = simpleDateFormat.parse(to);
            Page<ElectronicBill> electronicBills = electronicBillDao.findByStoreIdAndDateBetween(storeId, start, end, pageable);
            List<Object[]> electronicBillTotal = electronicBillDao.findTotalByStoreId(storeId, start, end);
            map.put("totalElements", electronicBills.getTotalElements());
            map.put("totalPages", electronicBills.getTotalPages());
            map.put("electronicBills", electronicBills.getContent());
          for (Object[] ob: electronicBillTotal){
              map.put("serviceChargeAmount",ob[0]);
              map.put("totalAmount", ob[1]);
              map.put("refundAmount", ob[2]);
              map.put("totalSingular", ob[3]);
          }

        } catch (ParseException e) {
            log.info("时间转换异常");
            e.printStackTrace();
        }
        return map;
    }
}
